package com.echoesnet.echoesshortvideo.Activities;

import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.ToastUtils;
import com.echoesnet.echoesshortvideo.ViewModels.LoginViewModel;
import com.echoesnet.echoesshortvideo.ViewModels.StepRegisterViewModel;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FindPassWordAct extends BaseActivity
{
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.verify_code)
    EditText edVerify;
    @BindView(R.id.nextOk)
    ImageView nextOk;
    @BindView(R.id.tvReshend)
    TextView tvReshend;
    @BindView(R.id.tvReshendTime)
    TextView tvReshendTime;
    @BindView(R.id.tvVerifyTime)
    TextView tvVerifyTime;
    @BindView(R.id.tvVerify)
    TextView tvVerify;
    private LoginViewModel lModel;
    private StepRegisterViewModel registerViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_pass_word);
        ButterKnife.bind(this);
        phone.setText("+86 " + getIntent().getStringExtra("phone"));


        edVerify.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                changeBtn();
            }

            @Override
            public void afterTextChanged(Editable s)
            {

            }
        });



        password.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                changeBtn();
            }

            @Override
            public void afterTextChanged(Editable s)
            {

            }
        });




        lModel = new LoginViewModel();
        lModel.getLoginLive().observe(this, new Observer<String>()
        {
            @Override
            public void onChanged(@Nullable String type)
            {
                if (type == null)
                    return;

                ToastUtils.showShort("新密码设置成功");

                finish();
            }
        });

        registerViewModel = new StepRegisterViewModel();
        registerViewModel.getLiveData().observe(this, new Observer<String>()
        {
            @Override
            public void onChanged(@Nullable String type)
            {
                if (type == null)
                    return;

                edVerify.setText(type);
            }
        });

        registerViewModel.sendVerificationCode(phone.getText().toString().replace("+86", "").trim(), "1");
        tvReshend.setEnabled(false);
        tvReshend.setTextColor(ContextCompat.getColor(this, R.color.v2));
        tvVerify.setEnabled(false);
        tvVerify.setTextColor(ContextCompat.getColor(this, R.color.v2));
        tvReshendTime.setVisibility(View.VISIBLE);
        ttMsgCountDown = new SecurityCountDown();
        MsgTimer.scheduleAtFixedRate(ttMsgCountDown, 0, 1000);

        nextOk.setClickable(false);
    }

    @OnClick({R.id.cancle, R.id.nextOk, R.id.tvVerify, R.id.tvReshend})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.cancle:
                finish();
                break;
            case R.id.nextOk:

                lModel.findPassWord(phone.getText().toString().replace("+86", "").trim(),
                        edVerify.getText().toString().trim(),
                        password.getText().toString().trim());
                break;
            case R.id.tvVerify:
                registerViewModel.sendVerificationCode(phone.getText().toString().replace("+86", "").trim(), "1");

                tvReshend.setEnabled(false);
                tvReshend.setTextColor(ContextCompat.getColor(this, R.color.v2));
                tvVerify.setEnabled(false);
                tvVerify.setTextColor(ContextCompat.getColor(this, R.color.v2));

                tvVerifyTime.setVisibility(View.VISIBLE);
                ttAudioMsgCountDown = new SoundCodeCountDown();
                AudioTimer.scheduleAtFixedRate(ttAudioMsgCountDown, 0, 1000);

                break;
            case R.id.tvReshend:
                registerViewModel.sendVoiceCode(phone.getText().toString().replace("+86", "").trim(), "1");
                tvReshend.setEnabled(false);
                tvReshend.setTextColor(ContextCompat.getColor(this, R.color.v2));
                tvVerify.setEnabled(false);
                tvVerify.setTextColor(ContextCompat.getColor(this, R.color.v2));
                tvReshendTime.setVisibility(View.VISIBLE);
                ttMsgCountDown = new SecurityCountDown();
                MsgTimer.scheduleAtFixedRate(ttMsgCountDown, 0, 1000);

                break;
        }
    }


    private TimerTask ttMsgCountDown;
    private TimerTask ttAudioMsgCountDown;
    private Timer MsgTimer=new Timer();
    private Timer AudioTimer=new Timer();
    private int msgCountdown=60;
    private  int  audioCountdown=60;

    /**
     * 验证码倒计时
     */
    private class SecurityCountDown extends TimerTask
    {
        @Override
        public void run()
        {
            runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    tvReshendTime.setText(String.format("%ss", String.valueOf(msgCountdown--)));
                    if (msgCountdown < 1)
                    {
                        resetGetSecurityButton();
                    }
                }
            });
        }
    }

    /**
     * 重置验证码获取按钮
     */
    private void resetGetSecurityButton()
    {
        tvReshend.setEnabled(true);
        tvReshend.setTextColor(ContextCompat.getColor(this, R.color.MC2));
        tvVerify.setEnabled(true);
        tvVerify.setTextColor(ContextCompat.getColor(this, R.color.MC2));
        tvReshendTime.setVisibility(View.GONE);
        msgCountdown = 60;
        if (ttMsgCountDown != null)
            ttMsgCountDown.cancel();

    }


    /**
     * 语音验证码倒计时
     */
    private class SoundCodeCountDown extends TimerTask
    {
        @Override
        public void run()
        {
            runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    tvVerifyTime.setText(String.format("%ss", String.valueOf(audioCountdown--)));
                    if (audioCountdown < 1)
                    {
                        resetGetSoundCodeButton();
                    }
                }
            });
        }
    }

    /**
     * 重置验证码获取按钮
     */
    private void resetGetSoundCodeButton()
    {
        tvVerify.setEnabled(true);
        tvVerify.setTextColor(ContextCompat.getColor(this, R.color.MC2));
        tvReshend.setEnabled(true);
        tvReshend.setTextColor(ContextCompat.getColor(this, R.color.MC2));
        tvVerifyTime.setVisibility(View.GONE);
        audioCountdown = 60;
        if (ttAudioMsgCountDown != null)
            ttAudioMsgCountDown.cancel();
    }

    private  void changeBtn()
    {
        if(edVerify.getText().toString().trim().length()<6||!CommonUtils.verifyInput(1, password.getText().toString().trim()))
        {
            nextOk.setClickable(false);
            nextOk.setAlpha(0.5f);
        }
        else
        {
            nextOk.setClickable(true);
            nextOk.setAlpha(1f);
        }
    }

}
